﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using System.IO;

using KELT;
using KELT.Viewport;

using KELT.KELTObjects;
using KELT.ErrorControl;
using KELT.ErrorControl.CustomException;

namespace KELT.Files
{
    public class FileKELT
    {
        #region Param
        public bool isSaved;
        public bool setDocumentName;
        public bool isLatestState;

        private string m_DocumentName;
        public string documentName
        {
            get { return m_DocumentName; }
            set { m_DocumentName = value; }
        }

        #region #filePathParam
        private string m_FileDirectory;
        public string fileDirectory
        {
            get { return m_FileDirectory; }
            set 
            { 
                m_FileDirectory = value;
                setFullPath();
            }
        }

        private string m_FileName;
        public string fileName
        {
            get { return m_FileName; }
            set 
            {
                m_FileName = value;
                setFullPath();
            }
        }

        private string m_FileExt;
        public string fileExt
        {
            get { return m_FileExt; }
            set
            {
                m_FileExt = value;
                setFullPath();
            }
        }

        private string m_FileFullPath;
        public string fileFullPath
        {
            get { return m_FileFullPath; }
        }

        public void setFullPath()
        {
            // m_FileExt 는 ".kelt" 형식임
            this.m_FileFullPath = m_FileDirectory + "\\" + m_FileName + m_FileExt;
        }

        public string tempFolderPath;
        #endregion

        #endregion

        public FileKELT()
        {
            isSaved = false;
            setDocumentName = false;
            isLatestState = false;
        }

        public FileKELT(string filePath)
        {
            isSaved = true;
            setDocumentName = true;
            isLatestState = true;

            this.m_FileFullPath = Path.GetFullPath(filePath);

            // TempFolder 자동으로 생성할 것. 

            Load();

        }

        public void init()
        {
            fileExt = ".kelt";

            if(fileFullPath != null)
            {
                this.fileExt = Path.GetExtension(fileFullPath);
                this.fileName = Path.GetFileNameWithoutExtension(fileFullPath);
                this.fileDirectory = Path.GetDirectoryName(fileFullPath);
            }
        }

        #region Save
        // Original Save Function
        public void Save()
        {
            // xTempFolderPath 에서 필요한 자료 모두 카피할 것.
            // 파일들 경로 새로 지정.
            try
            {
                XmlWriterSettings xmlSetting = new XmlWriterSettings();
                xmlSetting.Indent = true;
                xmlSetting.Encoding = Encoding.UTF8;
                /*
                * ViewPortMgr 내용 저장
                * 내부 Animation 도 함께 저장할 것.
                * ViewportObject.xml
                */
                ViewPortMgr viewPortMgr = (ViewPortMgr)Application.Current.FindResource("ViewportMgr");

                XmlWriter ViewportWriter = XmlWriter.Create(tempFolderPath + "\\Object\\ViewportObject.xml", xmlSetting);
                ViewportWriter.WriteStartDocument();
                viewPortMgr.GetViewportXML(ViewportWriter);
                ViewportWriter.WriteEndDocument();
                //ViewportWriter.Flush();
                ViewportWriter.Close();

                /*
                 * ObjectMgr 내용 저장
                 * LibraryObject.xml
                 */
                ObjManager objmgr = (ObjManager)Application.Current.FindResource("ObjectManager");  // 오브젝트 매니저

                XmlWriter LibraryWriter = XmlWriter.Create(tempFolderPath + "\\Object\\LibraryWriter.xml", xmlSetting);
                LibraryWriter.WriteStartDocument();
                objmgr.GetLibraryXML(LibraryWriter);
                LibraryWriter.WriteEndDocument();
                //LibraryWriter.Flush();
                LibraryWriter.Close();

                /*
                 * Page 내용 저장
                 * Page.xml
                 */

                XmlWriter PageWriter = XmlWriter.Create(tempFolderPath + "\\Timeline\\Page.xml", xmlSetting);
                PageWriter.WriteStartDocument();
                PageWriter.WriteStartElement("Page");
                viewPortMgr.GetPageXML(PageWriter);
                PageWriter.WriteEndElement();
                PageWriter.WriteEndDocument();
                //PageWriter.Flush();
                PageWriter.Close();

                /*
                 * Camera 저장
                 */
                XmlWriter CameraWriter = XmlWriter.Create(tempFolderPath + "\\Timeline\\Camera.xml", xmlSetting);
                CameraManager cammgr = (CameraManager)Application.Current.FindResource("CameraManager"); 
                CameraWriter.WriteStartDocument();
                cammgr.GetCameraXML(CameraWriter);
                CameraWriter.WriteEndDocument();
                //CameraWriter.Flush();
                CameraWriter.Close();
            }
            catch (Exception e)
            {
                ErrorManager.Instance.RecordError(e);
                throw e;
            }

            isSaved = true;
            isLatestState = true;
            setDocumentName = true;
        }
        
        /*
        public void Save(string documentName)
        {
            if(documentName.Length == 0)
            {
                throw new InvalidFileNameException();
            }

            this.documentName = documentName;
            this.setDocumentName = true;

            this.Save();
        }
         * */
        #endregion

        #region Load
        public void Load()
        {
            XmlReaderSettings xmlSetting = new XmlReaderSettings();
            xmlSetting.IgnoreComments = true;
            xmlSetting.IgnoreWhitespace = true;

            XmlReader ViewportReader = XmlReader.Create(tempFolderPath + "\\ViewportObject.xml", xmlSetting);

            ViewPortMgr viewPortMgr = (ViewPortMgr)Application.Current.FindResource("ViewportMgr");
            viewPortMgr.SetViewportFromXML(ViewportReader);
            /*            
            while(reader.Read())
            {
                if(reader.NodeType == XmlNodeType.Element && reader.Name == "NodeName")
                {
                    reader.Read();
                    string value = reader.Value;
                }
            }
            */
            ViewportReader.Close();

        }
        #endregion


    }
}
